<?php
namespace app\admin\controller\Sys\middleware;
use support\exception\ValidateException;
use app\admin\controller\Sys\model\Menu;
use app\admin\controller\Sys\model\Application;
use app\admin\controller\Sys\model\Field;
use app\admin\controller\Sys\model\Action;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
use think\facade\Db;



class deleteTable implements MiddlewareInterface
{
	
    public function process(Request $request, callable $handler) : Response
    {	
		$data = $request->post();
		$menuInfo = Menu::find($data['menu_id']);
		$applicationInfo = Application::find($menuInfo['app_id']);

		$connect = $menuInfo['connect'] ? $menuInfo['connect'] : config('database.default');
		$prefix = config('database.connections.'.$connect.'.prefix');
		
		if(config('database.connections.'.$connect.'.type') <> 'mysql'){
			return $handler($request);
		}
		
		if($menuInfo['table_name'] && $menuInfo['create_table']){
			Db::connect($connect)->execute('DROP TABLE if exists '.$prefix.$menuInfo['table_name']);
		}
		
		try{
			if(!empty($menuInfo['controller_name'])){
				$rootPath = base_path();
				@unlink($rootPath.'/app/'.$applicationInfo['app_dir'].'/controller/'.$menuInfo['controller_name'].'Controller.php');
				if($this->getSubFiles($rootPath.'/app/'.$applicationInfo['app_dir'].'/controller/'.explode('/',$menuInfo['controller_name'])[0])){
					remove_dir($rootPath.'/app/'.$applicationInfo['app_dir'].'/controller/'.explode('/',$menuInfo['controller_name'])[0]);
				}
				
				if(is_dir($rootPath.'/app/'.$applicationInfo['app_dir'].'/view/'.strtolower($menuInfo['controller_name']))){
					remove_dir($rootPath.'/app/'.$applicationInfo['app_dir'].'/view/'.strtolower($menuInfo['controller_name']));
				}
				if($this->getSubFiles($rootPath.'/app/'.$applicationInfo['app_dir'].'/view/'.strtolower(explode('/',$menuInfo['controller_name'])[0]))){
					remove_dir($rootPath.'/app/'.$applicationInfo['app_dir'].'/view/'.strtolower(explode('/',$menuInfo['controller_name'])[0]));
				}
				
				@unlink($rootPath.'/app/'.$applicationInfo['app_dir'].'/model/'.$menuInfo['controller_name'].'.php');
				if($this->getSubFiles($rootPath.'/app/'.$applicationInfo['app_dir'].'/model/'.explode('/',$menuInfo['controller_name'])[0])){
					remove_dir($rootPath.'/app/'.$applicationInfo['app_dir'].'/model/'.explode('/',$menuInfo['controller_name'])[0]);
				}
				
				@unlink($rootPath.'/app/'.$applicationInfo['app_dir'].'/validate/'.$menuInfo['controller_name'].'Validate.php');
				if($this->getSubFiles($rootPath.'/app/'.$applicationInfo['app_dir'].'/validate/'.explode('/',$menuInfo['controller_name'])[0])){
					remove_dir($rootPath.'/app/'.$applicationInfo['app_dir'].'/validate/'.explode('/',$menuInfo['controller_name'])[0]);
				}
				
				@unlink($rootPath.'/app/'.$applicationInfo['app_dir'].'/hook/'.$menuInfo['controller_name'].'.php');
				if($this->getSubFiles($rootPath.'/app/'.$applicationInfo['app_dir'].'/hook/'.explode('/',$menuInfo['controller_name'])[0])){
					remove_dir($rootPath.'/app/'.$applicationInfo['app_dir'].'/hook/'.explode('/',$menuInfo['controller_name'])[0]);
				}
				
				if(is_dir($rootPath.'/public/components/'.$applicationInfo['app_dir'].'/'.strtolower($menuInfo['controller_name']))){
					remove_dir($rootPath.'/public/components/'.$applicationInfo['app_dir'].'/'.strtolower($menuInfo['controller_name']));
				}
				
				if($this->getSubFiles($rootPath.'/public/components/'.$applicationInfo['app_dir'].'/'.strtolower(explode('/',$menuInfo['controller_name'])[0]))){
					remove_dir($rootPath.'/public/components/'.$applicationInfo['app_dir'].'/'.strtolower(explode('/',$menuInfo['controller_name'])[0]));
				}
			}
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		
		return $handler($request);
    }
	
	//判断当前目录有没有其他文件
	private function getSubFiles($filepath){
		if(is_dir($filepath)){
			$res = scandir($filepath);
			if(count($res) == 2){
				return true;
			}
		}
	}
	
}